﻿using Newtonsoft.Json;
using ServerMonitor.Helper;
using ServerMonitor.Helper.Currency;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ServerMonitor.Tool.MainInterface
{
    class SiteScanner
    {
        internal static void Start(ListBox listBox1)
        {
            foreach (string Line in listBox1.Items) {
              
                InitScanner(Line);
            }
            string Title = "运行监测" + DateTime.Now.ToString();
            string Content = "";
            foreach (string Line in listBox1.Items)
            {
                Console.WriteLine("开始扫描{0}",Line);

           Content+=     CreateLog(Line);
             
            }

            if (Content.IndexOf("<font") > 0)
                Title = "异常预警" + Title;
            SendScannerMail(Title, Content);
        }
        /// <summary>
        /// 根据上个方法，获取每个方法的数据
        /// </summary>
        /// <param name="line"></param>
        private static string CreateLog(string SiteDataName)
        {
            string ReadLinfo = StaticValue.SiteLogFloderPath + SiteDataName + DateTime.Now.ToString("yyyyMMdd");
            List<string> TempList = FileHelper.ReadAllLine(ReadLinfo);

            if (TempList.Count == 0)
                return string.Format("<p><font  color=\"red\">{0}发布了{1}个资源。</font></p>", SiteDataName, TempList.Count);
         
            else
                return string.Format("<p>{0}发布了{1}个资源。</p>", SiteDataName, TempList.Count);


        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="content"></param>
        private static void SendScannerMail(string Title,string content)
        {
            string Day = DateTime.Now.ToString("yyyyMMdd");
            string LogPath = StaticValue.BinPath + "MailLog.txt";
            List<string> RunlogListTemp = FileHelper.ReadAllLine(LogPath);
            if (!RunlogListTemp.Contains(Day))
                FileHelper.AppendUTF8Text(LogPath, DateTime.Now.ToString("yyyyMMdd"));

            else {
                Console.WriteLine("今天已发过，不重复发。");
                return;
            }
            string UserDataPath = StaticValue.BinPath + "Mail.json";
            try {
                string Content = FileHelper.ReadContextUtf8(UserDataPath);
           Mail.MailHelper.SendCatMailInfo   UserInfo= JsonConvert.DeserializeObject<Mail.MailHelper.SendCatMailInfo>(Content);

                Mail.MailHelper.SendMail(UserInfo,Title , content) ;
            } catch (Exception ex) { PrintLog.Log(ex);
                PrintLog.Log("发送邮件失败");
            }

        }

        /// <summary>
        /// 初始化扫描
        /// </summary>
        /// <param name="UserDataJson"></param>
        private static void InitScanner(string UserDataJson)
        {
            try
            {
                string ReadJson = FileHelper.ReadContextUtf8(StaticValue.UserInfoPath + UserDataJson + ".json");
                Tool.AddSite.AddHelper.UserData ALLJson = JsonConvert.DeserializeObject<Tool.AddSite.AddHelper.UserData>(ReadJson);
                string Shtml = WebHelper.HttpGet(ALLJson.WebLink1);
                List<string> TempList = HtmlHelper.GetLinkVlaueList(Shtml, ALLJson.Xpath1);
                foreach (string Line in TempList) {
                    string Temptext = Line + DateTime.Now.ToString("yyyyMMdd");
                    if (!StaticValue.LogList.Contains(Temptext))
                    {
                        Console.WriteLine("加入{0}",Line);
                        PrintLog.ReflushLog(StaticValue.UrlLogFile, StaticValue.LogList, Temptext);
                        FileHelper.AppendUTF8Text(StaticValue.SiteLogFloderPath + UserDataJson + DateTime.Now.ToString("yyyyMMdd"), Temptext);
                    }
               
                }

            }
            catch (Exception ex) {
                PrintLog.Log(ex);
            }

        }
    }
}
